﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using CustomsSystem.BLL;
using Maticsoft.Common;
using Microsoft.Win32;
using CustomsSystem.Model;

namespace WEB.T_all
{
    public partial class Lirun : System.Web.UI.Page
    {
        T_AllManage tALL = new T_AllManage();
        SheetInfoManage SM = new SheetInfoManage();
        DataSet ds = new DataSet();
        DataTable dt = new DataTable();
        LirunExcel dataToExcel = new LirunExcel();
        CustomerManage CM = new CustomerManage();

        protected void Page_Load(object sender, EventArgs e)
        {

            if (!IsPostBack)
            {
                //DateTime A = new DateTime(DateTime.Today.Year, DateTime.Today.AddMonths(1).Month, 1);
                //int month = Convert.ToInt32(A.Date.Month.ToString());
                
                tbGency.Text = DateTime.Now.AddMonths(-1).ToShortDateString ().ToString();
                tbGency1.Text = DateTime.Now.ToShortDateString().ToString();
                //rbt1.Checked = true;
                
            }
            bind();
            if (gvTALL.Rows.Count == 0)
            {
                Label7.Text = "没有数据";
            }
            else
            {
                Label7.Text = "";
            }
            this.tbGency.Attributes.Add("onClick", "new WdatePicker()");
            this.tbGency1.Attributes.Add("onClick", "new WdatePicker()");
        }
        /// <summary>
        /// 查询
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void Button3_Click(object sender, EventArgs e)
        {
            bind();
        }
    


        protected void bind()
        {
            DataSet ds = new DataSet();
            T_AllManage t_all = new T_AllManage();
            string payedID = "";
            payedID = "1";
            //if (rbt1.Checked == true)
            //{
            //    payedID = "1";
            //    gvTALL.DataSource = t_all.GetListLirun(Convert.ToDateTime(tbGency.Text), Convert.ToDateTime(tbGency1.Text), tbCus.Text, payedID);
            //    gvTALL.DataBind();
            //}
            //if (rbt2.Checked == true)
            //{
            //    payedID = " 0' or payedID='2 ";
            //    gvTALL.DataSource = t_all.GetListLirun(Convert.ToDateTime(tbGency.Text), Convert.ToDateTime(tbGency1.Text), tbCus.Text, payedID);
            //    gvTALL.DataBind();
            //}
            gvTALL.DataSource = t_all.GetListLirun(Convert.ToDateTime(tbGency.Text), Convert.ToDateTime(tbGency1.Text), tbCus.Text,payedID );
            gvTALL.DataBind();
            
        }

        [System.Web.Services.WebMethod]
        [System.Web.Script.Services.ScriptMethod]
        public static string[] GetCustomersName(string prefixText, int count)
        {
            CustomerManage cusManage = new CustomerManage();
            List<string> strArray = new List<string>();
            //string[] strArray = { "中国石油公司","中顺洁柔","中山技术",
            //"捷达捷达捷达达","时代复分","幅度为爱"};
            List<Customer> cusList = cusManage.GetModelList("");
            foreach (Customer aCus in cusList)
            {
                strArray.Add(aCus.CustomerName);
            }
            return (from m in strArray
                    where m.Contains(prefixText)
                    select m).Take(count).ToArray();
        }


        protected void gvTALL_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            decimal sum = 0, sum1 = 0;

            string str1 = "", str2 = "", str3 = "", str4 = "";
            int idxX = 0, idxDouHao1 = 0, idxDouHao2 = 0;
            string guiLiang = "";
            decimal heJi = 0;

            //计算利润
            for (int i = 0; i < gvTALL.Rows.Count; i++)
            {
                //计算柜量的合计
                guiLiang = gvTALL.Rows[i].Cells[7].Text;
                idxDouHao1 = guiLiang.IndexOf(",");
                if (idxDouHao1 != -1)
                {
                    str1 = guiLiang.Substring(0, idxDouHao1);
                    idxX = str1.IndexOf("X");
                    heJi += decimal.Parse(str1.Substring(0, idxX));
                    str2 = guiLiang.Substring(idxDouHao1 + 1);
                    idxDouHao2 = str2.IndexOf(",");
                    if (idxDouHao2 != -1)
                    {
                        str3 = str2.Substring(0, idxDouHao2);
                        str4 = str2.Substring(idxDouHao2 + 1);

                        idxX = str3.IndexOf("X");
                        heJi += decimal.Parse(str3.Substring(0, idxX));
                        idxX = str4.IndexOf("X");
                        heJi += decimal.Parse(str4.Substring(0, idxX));
                    }
                    else
                    {
                        idxX = str2.IndexOf("X");
                        heJi += decimal.Parse(str2.Substring(0, idxX));
                    }
                }
                else
                {
                    idxX = guiLiang.IndexOf("X");
                    heJi += decimal.Parse(guiLiang.Substring(0, idxX));
                }


                if (gvTALL.Rows[i].Cells[9].Text == "&nbsp;")
                {
                    gvTALL.Rows[i].Cells[9].Text ="0.00";
                }
                if (gvTALL.Rows[i].Cells[10].Text == "&nbsp;")
                {
                    gvTALL.Rows[i].Cells[10].Text = "0.00";
                }
                gvTALL.Rows[i].Cells[11].Text = Convert.ToString(Convert.ToDecimal(gvTALL.Rows[i].Cells[9].Text) - Convert.ToDecimal(gvTALL.Rows[i].Cells[10].Text));
                sum = sum + Convert.ToDecimal(gvTALL.Rows[i].Cells[9].Text);
                sum1 = sum1 + Convert.ToDecimal(gvTALL.Rows[i].Cells[10].Text);
            }
            Label1.Text = sum.ToString();
            Label2.Text = sum1.ToString();
            Label3.Text =Convert.ToString( sum - sum1);
            lblHeJi.Text = heJi.ToString();
        }

        protected void Excel_Click(object sender, EventArgs e)
        {
            DataSet ds = new DataSet();
            if (gvTALL.Rows.Count > 0)
            {
                DateTime KAI, MO;
                string payedID = "1";
                if (tbGency.Text == "")
                {
                    KAI = Convert.ToDateTime("1900-1-1").Date;
                }
                else
                {
                    KAI = Convert.ToDateTime(tbGency.Text).Date;
                }

                if (tbGency1.Text == "")
                {
                    MO = Convert.ToDateTime("2099-1-1").Date;
                }
                else
                    MO = Convert.ToDateTime(tbGency1.Text).Date;

                string Cname = tbCus.Text;
                //取得列名
                ds = tALL.GetListLirun(KAI, MO, Cname,payedID);
                foreach (DataColumn dc in ds.Tables[0].Columns)
                {
                    dt.Columns.Add(dc.ColumnName, dc.DataType);
                }
                //读取数据
                for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
                {
                    object[] arrdr = new object[ds.Tables[0].Columns.Count];
                    ds.Tables[0].Rows[i].ItemArray.CopyTo(arrdr, 0);
                    dt.Rows.Add(arrdr);

                }
                

                //生成excel
                string FilePath = Server.MapPath("~/document/Excel/");
                string file = dataToExcel.OutputExcel(dt, Cname, KAI, MO,payedID,  "利润统计表", FilePath);
                DownloadFile(file);
                return;
            }

            
        }


        protected void DownloadFile(string file)
        {
            string filename = "/document/Excel/" + file;
            int intStart = filename.LastIndexOf(@"\") + 1;
            string saveFileName = filename.Substring(intStart, filename.Length - intStart);

            System.IO.FileInfo fi = new System.IO.FileInfo(filename);
            string fileextname = fi.Extension;
            string DEFAULT_CONTENT_TYPE = "application/unknown";
            RegistryKey regkey, fileextkey;
            string filecontenttype;
            try
            {
                regkey = Registry.ClassesRoot;
                fileextkey = regkey.OpenSubKey(fileextname);
                filecontenttype = fileextkey.GetValue("Content Type", DEFAULT_CONTENT_TYPE).ToString();
            }
            catch
            {
                filecontenttype = DEFAULT_CONTENT_TYPE;
            }
            Response.Clear();
            Response.Charset = "gb2312";
            Response.Buffer = true;
            this.EnableViewState = false;
            Response.ContentEncoding = System.Text.Encoding.Default;
            //对文件进行编码，防止下载名出现乱码
            string encodefileName = System.Web.HttpUtility.UrlEncode(file, System.Text.Encoding.UTF8);

            Response.AppendHeader("Content-Disposition", "attachment;filename=" + encodefileName);
            Response.ContentType = filecontenttype;

            Response.WriteFile(filename);
            Response.Flush();
            Response.Close();

            Response.End();
        }

        protected void btqian_Click(object sender, EventArgs e)
        {
            Response.Redirect("FeeWS.aspx");
        }

        protected void btLirun_Click(object sender, EventArgs e)
        {
            Response.Redirect("Lirun.aspx");
        }

        protected void btfu_Click(object sender, EventArgs e)
        {
            Response.Redirect("YSfu.aspx");
        }

        protected void btshou_Click(object sender, EventArgs e)
        {
            Response.Redirect("YSshou.aspx");
        }

        protected void btWF_Click(object sender, EventArgs e)
        {
            Response.Redirect("YSfu.aspx");
        }

        protected void gvTALL_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            gvTALL.PageIndex = e.NewPageIndex;
            gvTALL.DataBind();
        }

        protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (DropDownList1.SelectedValue == "1")
            {
                Response.Redirect("FeeWS.aspx");
            }
        }
    }
}